package com.mostromas.ess.project.controller;

import com.arronlong.httpclientutil.common.util.StringUtil;
import com.mostromas.ess.entities.UpdateVersionInfo;
import com.mostromas.ess.feign.IFeignFileController;
import com.mostromas.ess.feign.IFeignProjectController;
import com.mostromas.ess.project.req.UpdateVersionREQ;
import com.mostromas.ess.project.service.IUpdateVersionService;
import com.mostromas.ess.util.base.Result;
import com.mostromas.ess.util.tools.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@Api(value = "版本更新管理接口", description = "提供软件版本的添加和删除")
@RestController
@RequestMapping("/version")
public class UpdateVersionController {

    @Autowired
    private IFeignProjectController feignProjectController;

    @Autowired
    private IUpdateVersionService updateVersionService;

    @PreAuthorize(("hasAuthority('version:search')"))
    @ApiOperation("根据客户端类型和版本号查询版本列表")
    @PostMapping("/search")
    public Result search(@RequestBody UpdateVersionREQ req) throws Exception {
        return updateVersionService.queryList(req);
    }

    @PreAuthorize(("hasAuthority('version:search')"))
    @ApiOperation("查询版本详情接口")
    @ApiImplicitParam(name = "id",value = "MD5",required = true)
    @GetMapping("/{id}")
    public Result view(@PathVariable("id") String id){
        return Result.ok(updateVersionService.getById(id));
    }

    @PreAuthorize(("hasAuthority('version:add')"))
    @ApiOperation("上传文件到OSS")
    @PostMapping("/file/upload")
    public Result upload(@RequestPart("file") MultipartFile file) throws IOException {
        return feignProjectController.upload(file);
    }

//    @PreAuthorize(("hasAuthority('version:delete')"))
//    @ApiOperation("删除OOS服务器的文件")
//    @ApiImplicitParam(name="fileUrl", value="要删除的文件URL", required=true)
//    @DeleteMapping("/file/delete")
//    public Result deleteFile(@RequestParam(value = "fileUrl", required = false) String fileUrl) {
//        return feignFileController.delete(fileUrl);
//    }

    @PreAuthorize(("hasAuthority('version:add')"))
    @ApiOperation("新增版本信息接口")
    @PostMapping
    public Result save(@RequestBody UpdateVersionInfo info){
        info.setPubTime(DateUtil.getTimestamp(new Date()));
        return Result.ok(updateVersionService.saveOrUpdate(info));
    }

    @PreAuthorize(("hasAuthority('version:delete')"))
    @ApiOperation("删除版本信息接口")
    @ApiImplicitParam(name = "md5",value = "版本MD5",required = true)
    @DeleteMapping("/{md5}")
    public Result delete(@PathVariable("md5") String id){
        String filePath = updateVersionService.getById(id).getUrl();
        if(StringUtils.isNotEmpty(filePath)){
            feignProjectController.delete(updateVersionService.getById(id).getUrl());
        }
        return Result.ok(updateVersionService.removeById(id));
    }
}